package com.tianjian.wordupload.controller;

import com.tianjian.wordupload.VO.ResultVO;
import com.tianjian.wordupload.utils.ResultVOUtil;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;

/**
 * @program: wordupload
 * @description: fileuploadmanager
 * @author: Mr.Caogp
 * @create: 2018-10-06 16:36
 **/
@RestController
public class FileController {

    private String uploadFolder = "/Users/apple/Documents/";

    @PostMapping("/upload")
    public ResultVO upload(@RequestParam(value = "file", required = false) MultipartFile file){




        if (file.isEmpty()) {
            System.out.println("文件为空");
           return ResultVOUtil.error("文件为空");
        }
        File uploadPath = new File(uploadFolder);
        try {

            System.out.println("上传的实际路径是"+uploadPath.getAbsolutePath());
            byte[] bytes = file.getBytes();
            //Path path = Paths.get(projectPath.getAbsolutePath()+"/static/"+file.getOriginalFilename());
            Path path = Paths.get(uploadPath.getAbsolutePath(),file.getOriginalFilename());
            Files.write(path, bytes);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return ResultVOUtil.success("上传成功");

    }
}
